﻿using Microsoft.AspNetCore.Mvc;
using Step4.Unit6.Models;
using Step4.Unit6.Services;
using Step4.Unit6.Services.Impl;
using System.Diagnostics;
namespace Step4.Unit6.Controllers
{
    /// <summary>
    /// 从IOC获取服务的三种方式：
    /// </summary>
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IFoodService _foodService;
        private readonly ICarService<String> _carService;

        // 第一种注入服务方式：  构造函数参数注入！！(这种注入方式用的最多)
        public HomeController(ILogger<HomeController> logger,  // ILogger<> 的泛型类型参数，可以在日志中显示出日志在哪里产生的！！
                              IFoodService foodService,
                              ICarService<string> carService)
        {
            _logger = logger;
            _foodService = foodService;
            _carService = carService;
        }


        public IActionResult Index()
        {
            // 第二种，通过HttpContext.RequestServices获取:   HttpContext.RequestServices属性数据类型为：IServiceProvider,所以可以获取服务实例！！  
            // 不能使用依赖注入的的环境，可以使用此方式
            Request.HttpContext.RequestServices.GetService(typeof(IFoodService));// 反射获取
            Request.HttpContext.RequestServices.GetService<IFoodService>(); // 泛型获取
            return View();
        }


        // 第三种，从方法参数注入(使用[FromServices]特性)
        public IActionResult Index2([FromServices] IFoodService foodService)
        {
            return View();
        }


        /// <summary>
        /// IOC中服务的生命周期测试
        /// </summary>
        /// <returns></returns>
        public IActionResult LifeTimeTest()
        {
            var _foodService2 = Request.HttpContext.RequestServices.GetService<IFoodService>();
            if (!object.ReferenceEquals(_foodService, _foodService2))
            {
                return Content("两次获取到的实例不同！");
            }
            else
            {
                return Content("两次获取到的服务实例相同！");
            }
        }


        /// <summary>
        ///Auto 泛型服务注册、注入测试
        /// </summary>
        /// <returns></returns>
        public IActionResult FanXingServiceTest()
        {
            return View();
        }


        public IActionResult AutofacTestNormalService() 
        {
            var result = _foodService.Print();
            if (string.IsNullOrEmpty(result))
            {
                return Content("Autofac注册的普通服务IFoodService,没有注册公共");
            }
            return Content("Autofac注册的普通服务IFoodService:" + result);
        }


        /// <summary>
        /// 测试使用Autofac注册的服务泛型注入
        /// </summary>
        /// <returns></returns>
        public IActionResult AutofacTest()
        {
            if (String.IsNullOrEmpty(_carService.PrintTType()))
            {
                return Content("使用Autofac注册的泛型服务，没有能够注入进来");
            }
            return Content("Autofac注入的的服务ICarService的PrintTType运行结果："+_carService.PrintTType());
        }

        /// <summary>
        /// 测试.net Core系统内置日志服务
        /// 测试前在Program.cs中把 log4net的配置注销掉！！
        /// </summary>
        /// <returns></returns>
        public IActionResult LoggerTest()
        {
            // 输出不同级别的日志
            _logger.LogTrace("trance日志测试");
            _logger.LogDebug("debug日志测试");
            _logger.LogInformation("information日志测试");
            _logger.LogWarning("warning日志测试");
            _logger.LogError("error日志测试");
            return View();
        }


        /// <summary>
        /// 测试log4net 文件日志组件
        /// 测试前在Program.cs中把 log4net的配置加上！！
        /// </summary>
        /// <returns></returns>
        public IActionResult Log4netTest()
        {
            // 输出不同级别的日志
            _logger.LogTrace("trance日志测试");
            _logger.LogDebug("debug日志测试");
            _logger.LogInformation("information日志测试");
            _logger.LogWarning("warning日志测试");
            _logger.LogError("error日志测试");
            return View();
        }



        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
